package com.lite.haru.alarmpolice.activity;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.kymjs.kjframe.ui.ViewInject;

import android.app.Activity;
import android.content.Intent;
import android.media.tv.TvContract.Programs;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.overlayutil.DrivingRouteOverlay;
import com.baidu.mapapi.overlayutil.OverlayManager;
import com.baidu.mapapi.overlayutil.TransitRouteOverlay;
import com.baidu.mapapi.overlayutil.WalkingRouteOverlay;
import com.baidu.mapapi.search.core.RouteLine;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.route.DrivingRouteLine;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteLine;
import com.baidu.mapapi.search.route.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteLine;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.baidu.navisdk.adapter.BNRoutePlanNode;
import com.baidu.navisdk.adapter.BaiduNaviManager;
import com.baidu.navisdk.adapter.BNRoutePlanNode.CoordinateType;
import com.baidu.navisdk.adapter.BaiduNaviManager.NaviInitListener;
import com.baidu.navisdk.adapter.BaiduNaviManager.RoutePlanListener;
import com.lite.haru.R;
import com.lite.haru.baidu_ditu.BNDemoGuideActivity;
import com.lite.haru.baidu_ditu.MyOrientationListener;
import com.lite.haru.baidu_ditu.MyOrientationListener.OnOrientationListener;
import com.lite.haru.constant.Constant;

/**
 * 此demo用来展示如何进行驾车、步行、公交路线搜索并在地图使用RouteOverlay、TransitOverlay绘制
 * 同时展示如何进行节点浏览并弹出泡泡
 */
public class AlarmRoutePlanAty extends Activity implements
		BaiduMap.OnMapClickListener, OnGetRoutePlanResultListener,
		OnClickListener {
	// 浏览路线节点相关
	private Button mBtnPre = null;// 上一个节点
	private Button mBtnNext = null;// 下一个节点
	private Button mBtnDrive = null;// 驾车路线
	private Button mBtnDriveNavi = null;// 驾车路线
	private Button mBtnTransit = null;// 公交路线
	private Button mBtnWalk = null;// 步行路线
	private ProgressBar mProgressBar = null;
	private TextView tv_progressBar = null;

	int nodeIndex = -1;// 节点索引,供浏览节点时使用
	RouteLine route = null;
	OverlayManager routeOverlay = null;
	boolean useDefaultIcon = true;
	private TextView popupText = null;// 泡泡view

	private LatLng stLL = null;
	private LatLng enLL = null;
	String authinfo = null;
	private String stCity = null;
	private String mSDCardPath = null;
	private LocationMode currentMode;// 当前定位的模式
	private BitmapDescriptor currentMarker = null; // 定位图标描述
	public LocationClient mLocationClient = null;// ???
	private volatile boolean isFristLocation = true;// 是否是第一次定位
	private float mCurrentAccracy;// 当前的精度
	public MyLocationListener mMyLocationListener;// 定位的监听器
	private MyOrientationListener myOrientationListener;// 方向传感器的监听器
	private int mXDirection;// 方向传感器X方向的值
	private LatLng ll;// 当前的经度与纬度
	private double mCurrentLantitude;// 最新一次的经纬度
	private double mCurrentLongitude;//
	private double mLatitude;// 接收上一层的纬度
	private double mLongitude;// 接收上一层的经度

	// 地图相关，使用继承MapView的MyRouteMapView目的是重写touch事件实现泡泡处理
	// 如果不处理touch事件，则无需继承，直接使用MapView即可
	MapView mMapView = null; // 地图View
	BaiduMap mBaidumap = null;
	// 搜索相关
	RoutePlanSearch mSearch = null; // 搜索模块，也可去掉地图模块独立使用

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_routeplan);
		initData();
		initWidget();
		// 初始化定位
		initMyLocation();
		// 初始化传感器
		initOritationListener();
	}

	/**
	 * 初始化数据
	 */
	public void initData() {
		Bundle bundle = this.getIntent().getExtras();
		mLatitude = bundle.getDouble("mLatitude");
		mLongitude = bundle.getDouble("mLongitude");
	}

	/**
	 * 初始化组件
	 */
	public void initWidget() {
		CharSequence titleLable = "路线规划功能";
		setTitle(titleLable);
		// 初始化地图
		mMapView = (MapView) findViewById(R.id.map);
		// 隐藏缩放控件
		mMapView.showZoomControls(false);
		// 第一次定位
		isFristLocation = true;
		mBaidumap = mMapView.getMap();
		MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(17.0f);
		mBaidumap.setMapStatus(msu);
		// 普通地图
		mBaidumap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
		// 跟随定位模式
		currentMode = LocationMode.NORMAL;
		mBaidumap.setMyLocationConfigeration(new MyLocationConfiguration(
				currentMode, true, currentMarker));
		mBtnPre = (Button) findViewById(R.id.pre);
		mBtnNext = (Button) findViewById(R.id.next);
		mBtnDrive = (Button) findViewById(R.id.drive);
		mBtnDriveNavi = (Button) findViewById(R.id.drive_navi);
		mBtnTransit = (Button) findViewById(R.id.transit);
		mBtnWalk = (Button) findViewById(R.id.walk);
		mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
		tv_progressBar = (TextView) findViewById(R.id.tv_progressBar);
		mBtnPre.setVisibility(View.INVISIBLE);
		mBtnNext.setVisibility(View.INVISIBLE);
		// 地图点击事件处理
		mBaidumap.setOnMapClickListener(this);
		// 初始化搜索模块，注册事件监听
		mSearch = RoutePlanSearch.newInstance();
		mSearch.setOnGetRoutePlanResultListener(this);
		mBtnDrive.setOnClickListener(this);
		mBtnDriveNavi.setOnClickListener(this);
		mBtnTransit.setOnClickListener(this);
		mBtnWalk.setOnClickListener(this);
	}

	/**
	 * 初始化方向传感器
	 */
	private void initOritationListener() {
		myOrientationListener = new MyOrientationListener(
				getApplicationContext());
		myOrientationListener
				.setOnOrientationListener(new OnOrientationListener() {
					@Override
					public void onOrientationChanged(float x) {
						mXDirection = (int) x;

						// 构造定位数据
						MyLocationData locData = new MyLocationData.Builder()
								.accuracy(mCurrentAccracy)
								// 此处设置开发者获取到的方向信息，顺时针0-360
								.direction(mXDirection)
								.latitude(mCurrentLantitude)
								.longitude(mCurrentLongitude).build();
						// 设置定位数据
						mBaidumap.setMyLocationData(locData);
					}
				});
	}

	/**
	 * 初始化定位相关代码
	 */
	private void initMyLocation() {
		// 定位初始化
		mLocationClient = new LocationClient(this);
		mMyLocationListener = new MyLocationListener();
		mLocationClient.registerLocationListener(mMyLocationListener);
		// 设置定位的相关配置
		LocationClientOption option = new LocationClientOption();
		option.setIsNeedAddress(true);
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型在百度地图上显示用"bd09ll"，在导航地图上显示用
										// "gcj02";
		option.setIsNeedLocationDescribe(true);
		option.setScanSpan(5000);
		mLocationClient.setLocOption(option);
	}

	/**
	 * 实现实位回调监听
	 */
	public class MyLocationListener implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {

			// map view 销毁后不在处理新接收的位置
			if (location == null || mMapView == null)
				return;
			if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果

				// Toast.makeText(
				// getApplicationContext(),
				// "GPS定位成功！" + location.getRadius() + "米" + " "
				// + location.getAddrStr() + " "
				// + location.getDistrict() + " "
				// + location.getTime(), Toast.LENGTH_LONG).show();

			} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果

				// Toast.makeText(
				// getApplicationContext(),
				// "网络定位成功！" + location.getRadius() + "米" + " "
				// + location.getAddrStr() + " "
				// + location.getDistrict() + " "
				// + location.getLocationDescribe()
				// + location.getLocationWhere(),
				// Toast.LENGTH_LONG).show();
			}

			stCity = location.getCity();

			// 构造定位数据
			MyLocationData locData = new MyLocationData.Builder()
					.accuracy(location.getRadius())
					// 此处设置开发者获取到的方向信息，顺时针0-360
					.direction(mXDirection).latitude(location.getLatitude())
					.longitude(location.getLongitude()).build();
			mCurrentAccracy = location.getRadius();
			// 设置定位数据
			mBaidumap.setMyLocationData(locData);
			mCurrentLantitude = location.getLatitude();
			mCurrentLongitude = location.getLongitude();

			// 第一次定位时的经纬度
			ll = new LatLng(mCurrentLantitude, mCurrentLongitude);
			// 第一次定位时，将地图位置移动到当前位置
			if (isFristLocation) {
				isFristLocation = false;
				MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
				mBaidumap.animateMapStatus(u);
			}
		}
	}

	/**
	 * 发起路线规划搜索示例
	 *
	 * @param v
	 */

	@Override
	public void onClick(View v) {
		// 重置浏览节点的路线数据
		route = null;
		mBtnPre.setVisibility(View.INVISIBLE);
		mBtnNext.setVisibility(View.INVISIBLE);
		mBaidumap.clear();
		// 处理搜索按钮响应
		EditText editSt = (EditText) findViewById(R.id.start);
		EditText editEn = (EditText) findViewById(R.id.end);
		// 设置起终点信息，对于tranist search 来说，城市名无意义
		PlanNode stNode = PlanNode.withCityNameAndPlaceName("盐城", editSt
				.getText().toString());
		PlanNode enNode = PlanNode.withCityNameAndPlaceName("盐城", editEn
				.getText().toString());
		// 33.359643
		stLL = new LatLng(mCurrentLantitude, mCurrentLongitude);
		enLL = new LatLng(mLatitude, mLongitude);

		PlanNode startNode = PlanNode.withLocation(stLL);
		PlanNode endNode = PlanNode.withLocation(enLL);

		// 实际使用中请对起点终点城市进行正确的设定

		switch (v.getId()) {
		case R.id.drive:
			mProgressBar.setVisibility(View.VISIBLE);
			tv_progressBar.setVisibility(View.VISIBLE);
			mBtnDrive.setBackground(getResources().getDrawable(
					R.drawable.button_down));
			mBtnTransit.setBackground(getResources().getDrawable(
					R.drawable.button_on));
			mBtnWalk.setBackground(getResources().getDrawable(
					R.drawable.button_on));
			mSearch.drivingSearch((new DrivingRoutePlanOption())
					.from(startNode).to(endNode));
			break;
		case R.id.drive_navi:
			// 导航
			if (initDirs()) {
				initNavi();
			}
			if (BaiduNaviManager.isNaviInited()) {
				routeplanToNavi(CoordinateType.GCJ02);
			}
			break;
		case R.id.transit:
			mProgressBar.setVisibility(View.VISIBLE);
			tv_progressBar.setVisibility(View.VISIBLE);
			mBtnDrive.setBackground(getResources().getDrawable(
					R.drawable.button_on));
			mBtnTransit.setBackground(getResources().getDrawable(
					R.drawable.button_down));
			mBtnWalk.setBackground(getResources().getDrawable(
					R.drawable.button_on));
			mSearch.transitSearch((new TransitRoutePlanOption())
					.from(startNode).city(stCity).to(endNode));
			break;
		case R.id.walk:
			mProgressBar.setVisibility(View.VISIBLE);
			tv_progressBar.setVisibility(View.VISIBLE);
			mBtnDrive.setBackground(getResources().getDrawable(
					R.drawable.button_on));
			mBtnTransit.setBackground(getResources().getDrawable(
					R.drawable.button_on));
			mBtnWalk.setBackground(getResources().getDrawable(
					R.drawable.button_down));
			mSearch.walkingSearch((new WalkingRoutePlanOption())
					.from(startNode).to(endNode));
			break;

		default:
			break;
		}

	}

	/**
	 * 节点浏览示例
	 *
	 * @param v
	 */
	public void nodeClick(View v) {
		if (route == null || route.getAllStep() == null) {
			return;
		}
		if (nodeIndex == -1 && v.getId() == R.id.pre) {
			return;
		}
		// 设置节点索引
		if (v.getId() == R.id.next) {
			if (nodeIndex < route.getAllStep().size() - 1) {
				nodeIndex++;
			} else {
				return;
			}
		} else if (v.getId() == R.id.pre) {
			if (nodeIndex > 0) {
				nodeIndex--;
			} else {
				return;
			}
		}
		// 获取节结果信息
		LatLng nodeLocation = null;
		String nodeTitle = null;
		Object step = route.getAllStep().get(nodeIndex);
		if (step instanceof DrivingRouteLine.DrivingStep) {
			nodeLocation = ((DrivingRouteLine.DrivingStep) step).getEntrance()
					.getLocation();
			nodeTitle = ((DrivingRouteLine.DrivingStep) step).getInstructions();
		} else if (step instanceof WalkingRouteLine.WalkingStep) {
			nodeLocation = ((WalkingRouteLine.WalkingStep) step).getEntrance()
					.getLocation();
			nodeTitle = ((WalkingRouteLine.WalkingStep) step).getInstructions();
		} else if (step instanceof TransitRouteLine.TransitStep) {
			nodeLocation = ((TransitRouteLine.TransitStep) step).getEntrance()
					.getLocation();
			nodeTitle = ((TransitRouteLine.TransitStep) step).getInstructions();
		}

		if (nodeLocation == null || nodeTitle == null) {
			return;
		}
		// 移动节点至中心
		mBaidumap.setMapStatus(MapStatusUpdateFactory.newLatLng(nodeLocation));
		// show popup
		popupText = new TextView(AlarmRoutePlanAty.this);
		popupText.setBackgroundResource(R.drawable.popup);
		popupText.setTextColor(0xFF000000);
		popupText.setText(nodeTitle);
		mBaidumap.showInfoWindow(new InfoWindow(popupText, nodeLocation, 0));

	}

	/**
	 * 切换路线图标，刷新地图使其生效 注意： 起终点图标使用中心对齐.
	 */
	public void changeRouteIcon(View v) {
		if (routeOverlay == null) {
			return;
		}
		if (useDefaultIcon) {
			((Button) v).setText("自定义起终点图标");
			Toast.makeText(this, "将使用系统起终点图标", Toast.LENGTH_SHORT).show();

		} else {
			((Button) v).setText("系统起终点图标");
			Toast.makeText(this, "将使用自定义起终点图标", Toast.LENGTH_SHORT).show();

		}
		useDefaultIcon = !useDefaultIcon;
		routeOverlay.removeFromMap();
		routeOverlay.addToMap();
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	public void onGetWalkingRouteResult(WalkingRouteResult result) {
		if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
			Toast.makeText(AlarmRoutePlanAty.this, "抱歉，未找到结果",
					Toast.LENGTH_SHORT).show();
			mProgressBar.setVisibility(View.GONE);
			tv_progressBar.setVisibility(View.GONE);
		}
		if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
			// 起终点或途经点地址有岐义，通过以下接口获取建议查询信息
			result.getSuggestAddrInfo();
			mProgressBar.setVisibility(View.GONE);
			tv_progressBar.setVisibility(View.GONE);
			return;
		}
		if (result.error == SearchResult.ERRORNO.NO_ERROR) {
			nodeIndex = -1;
			mBtnPre.setVisibility(View.VISIBLE);
			mBtnNext.setVisibility(View.VISIBLE);
			route = result.getRouteLines().get(0);
			WalkingRouteOverlay overlay = new MyWalkingRouteOverlay(mBaidumap);
			mBaidumap.setOnMarkerClickListener(overlay);
			routeOverlay = overlay;
			overlay.setData(result.getRouteLines().get(0));
			overlay.addToMap();
			overlay.zoomToSpan();
			mProgressBar.setVisibility(View.GONE);
			tv_progressBar.setVisibility(View.GONE);
		}

	}

	@Override
	public void onGetTransitRouteResult(TransitRouteResult result) {

		if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
			Toast.makeText(AlarmRoutePlanAty.this, "抱歉，未找到结果",
					Toast.LENGTH_SHORT).show();
			mProgressBar.setVisibility(View.GONE);
			tv_progressBar.setVisibility(View.GONE);
		}
		if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
			// 起终点或途经点地址有岐义，通过以下接口获取建议查询信息
			result.getSuggestAddrInfo();
			mProgressBar.setVisibility(View.GONE);
			tv_progressBar.setVisibility(View.GONE);
			return;
		}
		if (result.error == SearchResult.ERRORNO.NO_ERROR) {
			nodeIndex = -1;
			mBtnPre.setVisibility(View.VISIBLE);
			mBtnNext.setVisibility(View.VISIBLE);
			route = result.getRouteLines().get(0);
			TransitRouteOverlay overlay = new MyTransitRouteOverlay(mBaidumap);
			mBaidumap.setOnMarkerClickListener(overlay);
			routeOverlay = overlay;
			overlay.setData(result.getRouteLines().get(0));
			overlay.addToMap();
			overlay.zoomToSpan();
			mProgressBar.setVisibility(View.GONE);
			tv_progressBar.setVisibility(View.GONE);
		}
	}

	@Override
	public void onGetDrivingRouteResult(DrivingRouteResult result) {
		if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
			Toast.makeText(AlarmRoutePlanAty.this, "抱歉，未找到结果",
					Toast.LENGTH_SHORT).show();
			mProgressBar.setVisibility(View.GONE);
			tv_progressBar.setVisibility(View.GONE);
		}
		if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
			// 起终点或途经点地址有岐义，通过以下接口获取建议查询信息
			result.getSuggestAddrInfo();
			mProgressBar.setVisibility(View.GONE);
			tv_progressBar.setVisibility(View.GONE);
			return;
		}
		if (result.error == SearchResult.ERRORNO.NO_ERROR) {
			nodeIndex = -1;
			mBtnPre.setVisibility(View.VISIBLE);
			mBtnNext.setVisibility(View.VISIBLE);
			route = result.getRouteLines().get(0);
			DrivingRouteOverlay overlay = new MyDrivingRouteOverlay(mBaidumap);
			routeOverlay = overlay;
			mBaidumap.setOnMarkerClickListener(overlay);
			overlay.setData(result.getRouteLines().get(0));
			overlay.addToMap();
			overlay.zoomToSpan();
			mProgressBar.setVisibility(View.GONE);
			tv_progressBar.setVisibility(View.GONE);
		}
	}

	// 定制RouteOverly
	private class MyDrivingRouteOverlay extends DrivingRouteOverlay {

		public MyDrivingRouteOverlay(BaiduMap baiduMap) {
			super(baiduMap);
		}

		@Override
		public BitmapDescriptor getStartMarker() {
			if (useDefaultIcon) {
				return BitmapDescriptorFactory
						.fromResource(R.drawable.point_start_green);
			}
			return null;
		}

		@Override
		public BitmapDescriptor getTerminalMarker() {
			if (useDefaultIcon) {
				return BitmapDescriptorFactory
						.fromResource(R.drawable.point_end_red);
			}
			return null;
		}
	}

	private class MyWalkingRouteOverlay extends WalkingRouteOverlay {

		public MyWalkingRouteOverlay(BaiduMap baiduMap) {
			super(baiduMap);
		}

		@Override
		public BitmapDescriptor getStartMarker() {
			if (useDefaultIcon) {
				return BitmapDescriptorFactory
						.fromResource(R.drawable.point_start_green);
			}
			return null;
		}

		@Override
		public BitmapDescriptor getTerminalMarker() {
			if (useDefaultIcon) {
				return BitmapDescriptorFactory
						.fromResource(R.drawable.point_end_red);
			}
			return null;
		}
	}

	private class MyTransitRouteOverlay extends TransitRouteOverlay {

		public MyTransitRouteOverlay(BaiduMap baiduMap) {
			super(baiduMap);
		}

		@Override
		public BitmapDescriptor getStartMarker() {
			if (useDefaultIcon) {
				return BitmapDescriptorFactory
						.fromResource(R.drawable.point_start_green);
			}
			return null;
		}

		@Override
		public BitmapDescriptor getTerminalMarker() {
			if (useDefaultIcon) {
				return BitmapDescriptorFactory
						.fromResource(R.drawable.point_end_red);
			}
			return null;
		}
	}

	/**
	 * 地图移动到我的位置,此处可以重新发定位请求，然后定位； 直接拿最近一次经纬度，如果长时间没有定位成功，可能会显示效果不好
	 */
	// private void center2myLoc() {
	// LatLng ll = new LatLng(mCurrentLantitude, mCurrentLongitude);
	// MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
	// mBaidumap.animateMapStatus(u);
	// }

	// 校验百度导航引擎初始化
	private void initNavi() {
		BaiduNaviManager.getInstance().setNativeLibraryPath(
				mSDCardPath + "/BaiduNaviSDK_SO");
		BaiduNaviManager.getInstance().init(this, mSDCardPath,
				Constant.APP_FOLDER_NAME, new NaviInitListener() {
					@Override
					public void onAuthResult(int status, String msg) {
						if (0 == status) {
							authinfo = "key校验成功!";
						} else {
							authinfo = "key校验失败, " + msg;
						}
						AlarmRoutePlanAty.this.runOnUiThread(new Runnable() {

							@Override
							public void run() {
								ViewInject.toast(authinfo);
							}
						});
					}

					public void initSuccess() {
						ViewInject.toast("百度导航引擎初始化成功");

					}

					public void initStart() {

						ViewInject.toast("百度导航引擎初始化开始");
					}

					public void initFailed() {
						ViewInject.toast("百度导航引擎初始化失败");
					}
				}, null /* mTTSCallback */);
	}

	// 驾车路线导航函数实现
	private void routeplanToNavi(CoordinateType coType) {
		BNRoutePlanNode sNode = null;// 路径规划初始节点
		BNRoutePlanNode eNode = null;// 路径规划终点节点
		switch (coType) {
		case GCJ02: {

			// db存放的是当前的坐标
			// 要实现导航的准确性，把百度地图坐标转导航的坐标（ba09ll坐标--》GCJ02）
			BDLocation db = new BDLocation();
			db.setLongitude(mCurrentLongitude);
			db.setLatitude(mCurrentLantitude);
			// 将坐标转换成导航的经度坐标
			double eX = LocationClient.getBDLocationInCoorType(db,
					BDLocation.BDLOCATION_BD09LL_TO_GCJ02).getLongitude();
			// 将坐标转换成导航的纬度坐标
			double eY = LocationClient.getBDLocationInCoorType(db,
					BDLocation.BDLOCATION_BD09LL_TO_GCJ02).getLatitude();

			sNode = new BNRoutePlanNode(eX, eY, "出发地描述", null, coType);

			BDLocation db2 = new BDLocation();
			db2.setLongitude(mLongitude);
			db2.setLatitude(mLatitude);
			eNode = new BNRoutePlanNode(LocationClient.getBDLocationInCoorType(
					db2, BDLocation.BDLOCATION_BD09LL_TO_GCJ02).getLongitude(),
					LocationClient.getBDLocationInCoorType(db2,
							BDLocation.BDLOCATION_BD09LL_TO_GCJ02)
							.getLatitude(), "终点描述", null, coType);

			break;
		}
		default:
			;
		}
		if (sNode != null && eNode != null) {
			List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
			list.add(sNode);
			list.add(eNode);
			BaiduNaviManager.getInstance().launchNavigator(this, list, 1, true,
					new DemoRoutePlanListener(sNode));
		}
	}

	public class DemoRoutePlanListener implements RoutePlanListener {

		private BNRoutePlanNode mBNRoutePlanNode = null;

		public DemoRoutePlanListener(BNRoutePlanNode node) {
			mBNRoutePlanNode = node;
		}

		@Override
		public void onJumpToNavigator() {
			Intent intent = new Intent(AlarmRoutePlanAty.this,
					BNDemoGuideActivity.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable(Constant.ROUTE_PLAN_NODE,
					(BNRoutePlanNode) mBNRoutePlanNode);
			intent.putExtras(bundle);
			startActivity(intent);
		}

		@Override
		public void onRoutePlanFailed() {
			// TODO Auto-generated method stub

		}
	}

	private String getSdcardDir() {
		if (Environment.getExternalStorageState().equalsIgnoreCase(
				Environment.MEDIA_MOUNTED)) {
			return Environment.getExternalStorageDirectory().toString();
		}
		return null;
	}

	// 判断sd卡是否存在
	private boolean initDirs() {
		mSDCardPath = getSdcardDir();
		if (mSDCardPath == null) {
			return false;
		}
		File f = new File(mSDCardPath, Constant.APP_FOLDER_NAME);
		if (!f.exists()) {
			try {
				f.mkdir();
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}

	@Override
	public void onMapClick(LatLng point) {
		mBaidumap.hideInfoWindow();
	}

	@Override
	public boolean onMapPoiClick(MapPoi poi) {
		return false;
	}

	@Override
	protected void onStart() {
		// 开启图层定位
		mBaidumap.setMyLocationEnabled(true);
		if (!mLocationClient.isStarted()) {
			mLocationClient.start();
		}
		// 开启方向传感器
		myOrientationListener.start();
		super.onStart();

	}

	@Override
	protected void onStop() {
		// 关闭图层定位
		mBaidumap.setMyLocationEnabled(false);
		mLocationClient.stop();

		// 关闭方向传感器
		myOrientationListener.stop();
		super.onStop();
	}

	@Override
	protected void onPause() {
		mMapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		mMapView.onResume();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		mSearch.destroy();
		mMapView.onDestroy();
		super.onDestroy();
	}

}
